using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour, IInteractiveObject
{

    [SerializeField] private float speed;

    private Animator anim;
    private AudioSource audioWalk;
    private Rigidbody2D rb;
    private Vector3 moveVector;
    private bool canMove=true;
    [SerializeField] JoyStickMovment joyComponent;

    void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        audioWalk = GetComponent<AudioSource>();

        // PlayerData.ParentObject =  FindAnyObjectByType<ParentObject>(); //����������� �� ���������� �������
        PlayerData.ParentObject.Attach(this);
        //GameObject.FindGameObjectWithTag("ParentObject").GetComponent<ParentObject>().Attach(this);
        //PlayerData.Player = this;

        //SaveLoadManager.Load();

        PlayerData.ShowAllTAsk();
        //PlayerPrefs.DeleteAll();
    }

    void Update()
    {
        //������� ���� � PfrentObject ��� �������� ��������
        if (canMove)
        {
            Run();

            if (Input.GetKeyUp(KeyCode.J)) //������ �������
            {
                FindFirstObjectByType<Buttons>().OpenTaskList();
            }
            //else if (Input.GetKeyUp(KeyCode.B))
            //{
            //    FindFirstObjectByType<Buttons>().OpenInventory();
            //}
        }
    }

    private void Flip()
    {
        if (joyComponent.GetX() > 0)
        {
            Quaternion rotation = transform.rotation;
            rotation.y = 0;
            transform.rotation = rotation;
        }
        else if (joyComponent.GetX() < 0)
        {
            Quaternion rot = transform.rotation;
            rot.y = 180;
            transform.rotation = rot;
        }
    }

    private void Run()
    {
       // moveVector = Vector2.zero;
       // moveVector.x = Input.GetAxis("Horizontal");
       // moveVector.y = Input.GetAxis("Vertical");
        
        Flip();
        AnimationAudio();
        rb.MovePosition(transform.position + joyComponent.ReturnVectorDitection() * speed * Time.fixedDeltaTime);
    }

    private void AnimationAudio()
    {
        if (Vector3.zero != joyComponent.ReturnVectorDitection())
        {
            anim.SetBool("isRun", true);

            if (!audioWalk.isPlaying)
                audioWalk.Play();
        }
        else
        {
            anim.SetBool("isRun", false);
            audioWalk.Stop();
        }
    }

    public void OnCanInteractive(bool value)
    {
        canMove = value;
        anim.SetBool("isRun", false);
        audioWalk.Stop();
    }

    public void Sleeeeeep()
    {
        OnCanInteractive(false);
        anim.SetTrigger("Sleep");
    }
    //public void StopControl()
    //{
    //    canMove = false;
    //    anim.SetBool("isRun", false);
    //    audio.Stop();
    //}
    //���� ������
    //private void OnMouseDown()
    //{
    //    for (int i = 0; i < PlayerData.tasks.Count; i++)
    //    {
    //        if (PlayerData.tasks[i].name == "��������������")
    //            PlayerData.tasks[i].nps.DoneTask("��������������");
    //    }
    //}


}
